﻿using System;
using System.IO;
using System.Linq;
using System.Web;
using System.Threading;

namespace FX.CC.Andon.Utility.Common
{
    public class UploadFile
    {
        public static string InportUpLoadFile(HttpPostedFile fileNamePath, string tempFile)
        {
            const string fileType = "xls|xlsx";
            //获取图片格式
            string fileExt = fileNamePath.FileName.Substring(fileNamePath.FileName.LastIndexOf(".") + 1);
            //返回结果
            var result = string.Empty;
            //验证合法的文件
            if (CheckFileExt(fileType, fileExt))
            {
                //随机文件名
                var fileName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "." + fileExt;
                //按日期归类保存
                var datePath = DateTime.Now.ToString("yyyyMMdd") + "/";
                tempFile += "Upload\\" + datePath;
                //获得要保存的文件路径
                if (!Directory.Exists(tempFile))
                {
                    Directory.CreateDirectory(tempFile);
                }

                fileNamePath.SaveAs(string.Format("{0}{1}", tempFile, fileName));

                result = "Upload/" + datePath + fileName;
            }
            return result;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="fileNamePath">地址</param>
        /// <param name="tempFile">获得程序路径</param>
        /// <returns></returns>
        public static string UpLoadFile(HttpPostedFile fileNamePath, string tempFile)
        {
            const string fileType = "xls|xlsx|pdf";
            //获取图片格式
            string fileExt = fileNamePath.FileName.Substring(fileNamePath.FileName.LastIndexOf(".") + 1);
            //返回结果
            var result = string.Empty;
            //验证合法的文件
            if (CheckFileExt(fileType, fileExt))
            {
                //随机文件名
                var fileName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "." + fileExt;
                //按日期归类保存
                var datePath = "doc/";
                tempFile += "Upload\\" + datePath;
                //获得要保存的文件路径
                if (!Directory.Exists(tempFile))
                {
                    Directory.CreateDirectory(tempFile);
                }

                string fullFileName = string.Format("{0}{1}", tempFile, fileName);
                string pdfFileName = string.Format("{0}{1}{2}", tempFile, fileName, ".PDF");
                fullFileName = fullFileName.Replace("/", "\\");
                fileNamePath.SaveAs(fullFileName);
                if (fileExt != "pdf")
                {
                    ExcelComHelper.ExcelToPDF(fullFileName, pdfFileName);
                    File.Delete(fullFileName);
                    result = "Upload/" + datePath + fileName + ".PDF";
                }
                else
                {
                    result = "Upload/" + datePath + fileName;
                }
            }
            return result;
        }

        /// <summary>
        /// 检查是否为合法的上传文件
        /// </summary>
        /// <returns></returns>
        private static bool CheckFileExt(string _fileType, string _fileExt)
        {
            var allowExt = _fileType.Split('|');
            return allowExt.Any(t => t.ToLower() == _fileExt.ToLower());
        }
    }
}
